<template>
	<view class="content">
		<!-- #ifndef MP-TOUTIAO -->
		<navbar @getMsg="openMsg"></navbar>
		<!-- #endif -->
		<!-- <view class="text">安装如需帮助联系作者QQ：53600408</view> -->
		<view v-if="tabS" class="list-img" >
			<block v-for="(item,index) in list">
				<g-img :list="item"></g-img>
<!-- 				<view :data-id="item.img._id" @click="getImg" class="img-item">
					<image class="img" :src="item.img.url" mode=""></image>
				</view> -->
			</block>
		</view>
		<view v-if="tabL" class="list-tag">
			<block v-for="(item,index) in tag">
				<tag :tag="item"></tag>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				off:false,
				title: 'Hello',
				list:null,
				tag:null,
				tabS:true,
				tabL:false,
				total:null,
				skipL:10
			}
		},
		onLoad() {
			this.getTag()
			console.log(Date.now(),'sssjjjj')
			this.getCount()
			this.getCode()
		},
		methods: {
			getCode(){
				const then = this
				uni.login({
					provider:'weixin',
					  success: function (loginRes) {
					    console.log(loginRes.code);
						const code = loginRes.code
						this.code = loginRes.code
						then.getOPen(loginRes.code)
					  }
				})
			},
			getOPen(code){
				//const code = this.code
				uniCloud.callFunction({
					name: 'bzPen',
					data: {
						code,
					}
				}).then(res=>{
					console.log(res.result.data.openid,"fanhui")
					const open = res.result.data.openid
					uni.setStorageSync('open',open)
					this.openid = open
					const db = uniCloud.database();
					db.collection('bz-open').where({
						"open":open
					}).get().then(res=>{
						console.log(res.result.data.length,"查询")
						if(res.result.data.length==0){
							db.collection('bz-open').add({
								"open":open
							}).then(res=>{
								console.log("添加新用户")
							})
						}
					})

				})
			},
			// 瀑布流
			getCount(){
				const db = uniCloud.database();
				db.collection('bz-list').count()
				.then(res=>{
					this.total = res.result.total
					this.getList()
				})
			},
			//获取初始数据
			getList(){
				const db = uniCloud.database();
				db.collection('bz-list').orderBy("img.sjc", "desc").limit(10).get()
				.then(res=>{
					this.list = res.result.data
				})
			},
			// 切换虚拟tab
			getImg(){
				console.log(this.tabS)
			},
			openMsg(msg){
				console.log(msg)
				if(msg==0){
					this.tabS = !this.tabS
					this.tabL = !this.tabL
					this.getTag()
				}else if(msg==1){
					this.tabL = !this.tabL
					this.tabS = !this.tabS
				}
			},
			//获取tag
			getTag(){
				const db = uniCloud.database();
				db.collection('bz-tag').get()
				.then(res=>{
					console.log(res.result.data,'tag')
					this.tag = res.result.data
				})
			},
		},
		onReachBottom() {
			if(this.skipL>this.total){
				uni.showToast({
					icon:'none',
					title:'已加载完成'
				})
			}else{
				const db = uniCloud.database();
				const ye = 10;
				db.collection('bz-list').skip(this.skipL).orderBy("img.sjc", "desc").limit(10).get()
				.then(res=>{
					console.log(res.result.data,'count')
					this.skipL =this.skipL+10
					console.log('页数',this.skipL)
					const arr = res.result.data
					const brr = this.list
					this.list = brr.concat(arr)
					console.log(this.list,'zuixin')
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		.list-img{
			padding-top: 18rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
			.img{
				border-radius: 15rpx;
				width: 240rpx;
				height: 420rpx;
			}
		}
	}
</style>
